<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <input type="file" @change="onChange" hidden ref="inputFile" />
    <van-cell title="头像" is-link>
      <van-image
        @click="$refs.inputFile.click()"
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
      />
    </van-cell>
    <van-cell
      @click="isShowUpdateName = true"
      title="昵称"
      :value="user.name"
      is-link
    />
    <van-cell
      @click="isShowUpdateGender = true"
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
    />
    <van-cell
      @click="isShowUpdateBirthdy = true"
      title="生日"
      :value="user.birthday"
      is-link
    />
    <!-- <van-cell title="手机" :value="user.mobile" is-link /> -->

    <!-- 编辑昵称弹层 -->
    <van-popup
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom"
    >
      <updateName
        v-model="user.name"
        v-if="isShowUpdateName"
        @close="isShowUpdateName = false"
      ></updateName>
    </van-popup>
    <!-- 编辑昵称弹层 -->

    <!-- 编辑性别弹层 -->
    <van-popup v-model="isShowUpdateGender" position="bottom">
      <update-gender
        v-if="isShowUpdateGender"
        @close="isShowUpdateGender = false"
        v-model="user.gender"
      />
    </van-popup>
    <!-- 编辑昵称弹层 -->

    <!-- 编辑生日弹层 -->
    <van-popup v-model="isShowUpdateBirthdy" position="bottom">
      <update-birthdy
        v-if="isShowUpdateBirthdy"
        @close="isShowUpdateBirthdy = false"
        v-model="user.birthday"
      />
    </van-popup>
    <!-- 编辑生日弹层 -->

    <!-- 编辑图像弹层 -->
    <van-popup
      v-model="isShowUpdatePhoto"
      style="height: 100%"
      position="bottom"
    >
      <updateAvatar
        v-if="isShowUpdatePhoto"
        @update-avatar="user.photo = $event"
        :img="img"
        @close="isShowUpdatePhoto = false"
      ></updateAvatar>
    </van-popup>
    <!-- 编辑图像弹层 -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import updateName from './components/update-name.vue'
import updateGender from './components/update-gender.vue'
import updateBirthdy from './components/update-birthdy.vue'
import updateAvatar from './components/update-avatar.vue'
export default {
  name: 'userProfile',
  components: {
    updateName,
    updateGender,
    updateBirthdy,
    updateAvatar
  },
  data () {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthdy: false,
      isShowUpdatePhoto: false,
      img: ''
    }
  },

  created () {
    this.loadProfile()
  },

  methods: {
    async loadProfile () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (err) {
        this.$toast('用户未登录或请求数据失败')
      }
    },
    // 选择头像事件
    onChange () {
      // 拿到选取的图片
      const file = this.$refs.inputFile.files[0]
      // 把图片地址转换成需要的格式
      // const data = window.URL.createObjectURL(file)
      this.img = window.URL.createObjectURL(file)
      this.isShowUpdatePhoto = true
      this.$refs.inputFile.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
.van-popup {
  background: #f5f7f9;
}
</style>
